/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataAccessObject;
import java.sql.*;

/**
 *
 * @author ngoctoan
 */
public class SQLServerDAO {
        private Connection con;
    public SQLServerDAO() throws Exception {
//               
//        Laptop
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
        con = DriverManager.getConnection("jdbc:sqlserver://ngoctoan-PC\\SQLEXPRESS:1433;databaseName=supermarket", "netbean", "java");        
        
    }
    
    public void DongKetNoi() {
        try {
            if(con!=null) {
                con.close();
            }                
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }
    
    public ResultSet LayDuLieu(String query) throws SQLException{
//        System.out.println("DATABASE NAME IS:" + con.getMetaData().getDatabaseProductName());
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query);
        return rs;
    }
    
    public int UpdateQuery (String query, Object p[]) {
        try {
            PreparedStatement ps = con.prepareStatement(query);
            for (int i = 0; i<p.length;i++) {
                ps.setObject(i+1, p[i]);
            }
            return ps.executeUpdate();
        }
        catch (Exception e) {
            System.out.println("XoaDuLieu Error:" + e);
        }
        return 0;
    }
}
